<?php

namespace catchAdmin\visitPlan\controller;

use app\common\lib\ZhyxBeanstalkd;
use catchAdmin\visitPlan\request\CreateRequest;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\visitPlan\model\VisitPlan as VisitPlanModel;
use catcher\exceptions\FailedException;
use Pheanstalk\JobId;
use think\Cache;
use think\Exception;
use think\Response;

class VisitPlan extends CatchController
{

    protected $visitPlanModel;

    /**
     *
     * @time 2022/10/17 14:39
     * @param VisitPlanModel $visitPlanModel
     * @return mixed
     */
    public function __construct(VisitPlanModel $visitPlanModel)
    {
        $this->visitPlanModel = $visitPlanModel;
    }

    /**
     *
     * @time 2022/10/17 14:39
     * @return Response
     */
    public function index(): Response
    {
        $result = $this->visitPlanModel->getLists();
        if (count($result->items()) > 0) {
            $result->append(["push_type_text", "status_text", "can_delete", "period_text"]);
        }
        return CatchResponse::paginate($result);
    }

    /**
     *
     * @time 2022/10/17 14:39
     * @param Request $request
     * @return Response
     */
    public function save(CreateRequest $request): Response
    {
        try {
            $user = $request->user();
            $userDept = $user->userDept ?? [];
            $postData = $request->param();
            $postData["creator_dept_name"] = $userDept["department_name"] ?? "";
            $postData["creator_dept_id"] = $userDept["id"] ?? 0;
            $postData["creator_name"] = $user->username ?? "";
            $postData["is_all"] = $postData["is_all"] ?? false;
            $result = $this->visitPlanModel->createData($postData);
            if ($result) {
                return CatchResponse::success();
            }
            return CatchResponse::fail("创建失败");
        } catch (\Exception $exception) {
            throw new FailedException($exception->getMessage());
        }


    }

    /**
     *
     * @time 2022/10/17 14:39
     * @param $id
     * @return Response
     */
    public function read($id): Response
    {
        return CatchResponse::success($this->visitPlanModel->findBy($id));
    }

    /**
     *
     * @time 2022/10/17 14:39
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update($id, Request $request): Response
    {
        return CatchResponse::success($this->visitPlanModel->updateBy($id, $request->post()));
    }

    /**
     *
     * @time 2022/10/17 14:39
     * @param $id
     * @return Response
     */
    public function delete($id): Response
    {
        $visitInfo = $this->visitPlanModel->find($id);
        $beforeTime = date("Y-m-d", strtotime("-" . $visitInfo['remind_count'] . " day", strtotime($visitInfo["next_visit_time"])));
        $diffDays = date_diff(date_create($beforeTime), date_create(date("Y-m-d")), false);
        if ($diffDays->days > 0 && $diffDays->invert === 1) {
            ZhyxBeanstalkd::getInstance()->beansTalk->delete(new JobId($visitInfo["queue_id"]));
            return CatchResponse::success($this->visitPlanModel->deleteBy($id));
        }
        return CatchResponse::fail("已推送给患者，不能进行删除操作");

    }
}